package couk.mmtdigital.sm.control.commands
{
	import couk.mmtdigital.sm.ApplicationFacade;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	public class LogoutCommand extends SimpleCommand implements ICommand
	{
		private var logoutRO:RemoteObject;
		
		override public function execute(notification:INotification):void{
			logoutRO = new RemoteObject("spreadsheetServices");
			logoutRO.source = "sm.UserConfig";
			logoutRO.addEventListener(FaultEvent.FAULT, faultHandler);
			logoutRO.logoutUser.addEventListener(ResultEvent.RESULT, resultHandler);
			logoutRO.logoutUser();
		}
		
		private function faultHandler(fault:FaultEvent):void
        {
        	trace("code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail);
        }
        
        private function resultHandler(evt:ResultEvent):void{
        	if(evt.result == "logged out"){
        		sendNotification(ApplicationFacade.LOGGEDOUT);
        	}
        }
	}
}